1.7 Operators and Expressions in C
Module 1.7 • Arithmetic, Relational, Logical Evaluation Mappings & Precedence Rules
Operators are special symbols used to perform operations on variables, constants, and values. For example, when you add two numbers, compare values, or check multiple conditions, operators are used.
An operand is the value on which an operator performs an operation.
Example:
x + y
Here:
+is an operatorxandyare operands
The combination of operands and operators forms an expression.
Why Are Operators Important?
Operators help us:
- Perform calculations
- Compare values
- Make decisions
- Manipulate data
- Control program flow
Without operators, programs cannot perform meaningful work.
Categories of Operators in C
C provides several types of operators:
- Arithmetic Operators
- Assignment Operators
- Increment and Decrement Operators
- Relational Operators
- Logical Operators
- Conditional Operator
- Comma Operator
- sizeof Operator
- Bitwise Operators
- Type Cast Operator
1.7.1 Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Purpose |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Remainder) |
Arithmetic Example
#include <stdio.h>
int main()
{
int a = 24;
int b = 5;
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
printf("Division = %d\n", a / b);
printf("Remainder = %d\n", a % b);
return 0;
}
Output
Addition = 29
Subtraction = 19
Multiplication = 120
Division = 4
Remainder = 4
Unary Arithmetic Operators
Unary operators work on a single operand.
Example
+x
-y
The unary minus changes the sign of the value.
Integer Arithmetic
When both operands are integers, the result is also an integer.
Example:
int a = 19;
int b = 4;
| Expression | Result |
|---|---|
| a+b | 23 |
| a-b | 15 |
| a*b | 76 |
| a/b | 4 |
| a%b | 3 |
Notice that:
19 / 4
gives:
4
because the decimal part is discarded.
Floating Point Arithmetic
When operands are float values, decimal values are preserved.
Example:
float x = 18.5;
float y = 2.5;
| Expression | Result |
|---|---|
| x+y | 21.0 |
| x-y | 16.0 |
| x*y | 46.25 |
| x/y | 7.4 |
Mixed Mode Arithmetic
When one operand is an integer and the other is a floating-point value, the result becomes a floating-point value.
Example:
int a = 12;
float b = 4.0;
a / b
Result:
3.0
1.7.2 Assignment Operators
Assignment operators are used to store values in variables.
Simple Assignment
int marks;
marks = 85;
The value 85 is assigned to the variable marks.
Compound Assignment Operators
These operators make code shorter.
| Operator | Equivalent Form |
|---|---|
+= | x = x + value |
-= | x = x - value |
*= | x = x * value |
/= | x = x / value |
%= | x = x % value |
Example
int total = 50;
total += 10;
Equivalent to:
total = total + 10;
Result:
60
1.7.3 Increment and Decrement Operators
These operators increase or decrease a variable's value by 1.
| Operator | Meaning |
|---|---|
++ | Increment |
-- | Decrement |
Prefix Increment
The value is updated first and then used.
int x = 6;
int y = ++x;
Result:
x = 7
y = 7
Postfix Increment
The value is used first and then updated.
int x = 6;
int y = x++;
Result:
x = 7
y = 6
Prefix Decrement
int n = 10;
int m = --n;
Result:
n = 9
m = 9
Postfix Decrement
int n = 10;
int m = n--;
Result:
n = 9
m = 10
1.7.4 Relational Operators
Relational operators compare two values. The result is:
- 1 → True
- 0 → False
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not Equal to |
> | Greater Than |
< | Less Than |
>= | Greater Than or Equal To |
<= | Less Than or Equal To |
Example
int a = 15;
int b = 10;
printf("%d", a > b);
Output:
1
Because 15 is greater than 10.
More Examples
| Expression | Result |
|---|---|
| 15 > 10 | 1 |
| 15 < 10 | 0 |
| 15 == 10 | 0 |
| 15 != 10 | 1 |
| 15 >= 15 | 1 |
Difference Between = and ==
Many beginners confuse these operators.
Assignment Operator
x = 5;
Assigns value 5 to x.
Equality Operator
x == 5
Checks whether x is equal to 5.
1.7.5 Logical Operators
Logical operators combine multiple conditions.
| Operator | Meaning |
|---|---|
&& | AND |
|| | OR |
! | NOT |
Logical AND (&&)
Both conditions must be true.
int age = 25;
int salary = 40000;
(age > 18) && (salary > 30000)
Result:
1
Logical OR (||)
At least one condition must be true.
(age > 30) || (salary > 30000)
Result:
1
Logical NOT (!)
Reverses the condition.
!(5 > 2)
Result:
0
Because 5 > 2 is already true.
1.7.6 Conditional (Ternary) Operator
The ternary operator is a short form of if-else.
Syntax
condition ? expression1 : expression2;
Example
int x = 18;
int y = 30;
int larger = (x > y) ? x : y;
Result:
30
Practical Example
int age = 20;
(age >= 18) ? printf("Eligible") : printf("Not Eligible");
Output:
Eligible
1.7.7 Comma Operator
The comma operator allows multiple expressions to be written together.
Example
int result;
result = (2, 4, 6, 8);
The final value becomes:
8
because the value of the last expression is returned.
Another Example
int total;
total = (a = 4, b = 6, a + b);
Result:
10
1.7.8 sizeof Operator
The sizeof operator returns the memory occupied by a data type or variable.
Syntax
sizeof(data_type)
or
sizeof(variable)
Example
printf("%zu", sizeof(int));
Possible Output:
4
More Examples
sizeof(char)
sizeof(float)
sizeof(double)
These return the size in bytes.
Why sizeof Is Useful
It helps:
- Write portable programs
- Allocate memory correctly
- Understand memory usage
1.7.9 Bitwise Operators
Bitwise operators work directly on binary bits.
| Operator | Meaning |
|---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left Shift |
>> | Right Shift |
Example
int a = 6;
int b = 3;
Binary values:
6 = 0110
3 = 0011
Bitwise AND
a & b
Result:
2
Bitwise OR
a | b
Result:
7
Left Shift
a << 1
Result:
12
1.7.10 Type Conversion
Sometimes C automatically converts one data type into another. This process is called Type Conversion.
Automatic Type Conversion
int x = 15;
float y = 2.5;
float result = x + y;
The integer value is automatically converted into float.
Result:
17.5
Type Conversion in Assignment
float marks = 95.75;
int total = marks;
Result:
95
The decimal portion is removed.
Explicit Type Conversion (Type Casting)
Sometimes we manually convert data types.
Syntax
(data_type) expression
Example
int a = 9;
int b = 2;
float result;
result = (float)a / b;
Output:
4.5
Without type casting:
result = a / b;
Output:
4.0
because integer division occurs first.
Operator Precedence
When an expression contains multiple operators, C follows precedence rules.
Example:
4 + 3 * 5
Multiplication is performed first.
Result:
19
not 35.
Using Parentheses
Parentheses can change evaluation order.
(4 + 3) * 5
Result:
35
Associativity
When operators have the same precedence, associativity decides the order.
Example:
18 - 5 - 3
Evaluated from left to right.
13 - 3 = 10
Result:
10
Summary
- Operators perform actions on data and variables.
- Arithmetic operators perform calculations.
- Assignment operators store values.
- Increment and decrement operators modify values by 1.
- Relational operators compare values.
- Logical operators combine conditions.
- The ternary operator provides a compact alternative to if-else.
- The comma operator evaluates multiple expressions.
- sizeof returns memory size in bytes.
- Bitwise operators work on binary values.
- Type conversion changes data from one type to another.
- Precedence and associativity determine how expressions are evaluated.
- Parentheses can be used to control evaluation order.
1.7.11 Operator Precedence and Associativity
When an expression contains multiple operators, C follows a set of rules to decide:
- Which operator should be evaluated first (Precedence)
- In which direction operators of the same precedence should be evaluated (Associativity)
These rules ensure that every expression produces a predictable result.
What is Operator Precedence?
Operator precedence determines the priority of operators. Operators with higher precedence are evaluated before operators with lower precedence.
Example
5 + 4 * 3
Multiplication has higher precedence than addition.
Evaluation:
5 + 12
= 17
Output:
17
What is Associativity?
Associativity determines the evaluation order when operators have the same precedence. Associativity can be:
- Left to Right
- Right to Left
Example
20 - 5 - 3
Subtraction has Left-to-Right associativity.
Evaluation:
20 - 5 = 15
15 - 3 = 12
Output:
12
Common Operator Precedence Table
The following table lists commonly used operators from highest priority to lowest priority.
| Precedence Level | Operators | Description | Associativity |
|---|---|---|---|
| 1 | () [] -> . | Function call, array access, member access | Left to Right |
| 2 | ++ -- + - ! ~ * & (type) sizeof | Unary operators | Right to Left |
| 3 | * / % | Multiplication, Division, Modulus | Left to Right |
| 4 | + - | Addition, Subtraction | Left to Right |
| 5 | << >> | Bitwise Shift Operators | Left to Right |
| 6 | < <= > >= | Relational Operators | Left to Right |
| 7 | == != | Equality Operators | Left to Right |
| 8 | & | Bitwise AND | Left to Right |
| 9 | ^ | Bitwise XOR | Left to Right |
| 10 | | | Bitwise OR | Left to Right |
| 11 | && | Logical AND | Left to Right |
| 12 | || | Logical OR | Left to Right |
| 13 | ?: | Conditional Operator | Right to Left |
| 14 | = += -= *= /= %= &= ^= |= <<= >>= | Assignment Operators | Right to Left |
| 15 | , | Comma Operator | Left to Right |
Example 1: Arithmetic Precedence
int result;
result = 12 + 4 * 2;
Evaluation:
12 + 8
= 20
Output:
20
Example 2: Multiple Operators
int value;
value = 20 - 8 / 2;
Evaluation:
20 - 4
= 16
Output:
16
Division is performed before subtraction.
Example 3: Relational and Arithmetic Operators
int result;
result = 8 + 2 > 5;
Evaluation:
10 > 5
Output:
1
Because the condition is true.
Example 4: Assignment Associativity
int a, b, c;
a = b = c = 50;
Evaluation occurs from Right to Left.
c = 50
b = 50
a = 50
Final values:
a = 50
b = 50
c = 50
Conditional Operator Associativity
The conditional operator is Right-to-Left associative.
Example:
result = x ? y : z ? a : b;
This is interpreted as:
result = x ? y : (z ? a : b);
Using Parentheses to Control Evaluation
Parentheses have the highest priority and can change the order of evaluation.
Without Parentheses
24 / 4 + 2
Evaluation:
6 + 2
= 8
With Parentheses
24 / (4 + 2)
Evaluation:
24 / 6
= 4
Output:
4
Nested Parentheses
Expressions inside the innermost parentheses are evaluated first.
Example:
(5 * (3 + 4))
Evaluation:
(5 * 7)
= 35
Output:
35
Complex Expression Example
Assume:
int a = 6;
int b = 3;
int c = 2;
int d = 4;
Expression:
a + b * c - d
Evaluation:
6 + 6 - 4
12 - 4
8
Output:
8
Order of Evaluation of Operands
In most expressions, C does not define the order in which operands are evaluated.
Example:
y = (++x) + (--x);
The result may vary between compilers because the order of evaluating the two operands is not guaranteed. For this reason, such expressions should be avoided.
Operators with Guaranteed Evaluation Order
C guarantees left-to-right evaluation for:
- Logical AND (&&)
- Logical OR (||)
- Conditional Operator (?:)
- Comma Operator (,)
Example:
(a > b) && (c > d)
The left condition is always checked first.
Best Practices
- Use parentheses when expressions become complex.
- Avoid writing expressions that modify the same variable multiple times.
- Do not rely on compiler-specific evaluation behavior.
- Use meaningful intermediate variables to improve readability.
- Remember that precedence decides priority, while associativity decides direction.
Summary
- Precedence determines which operator is executed first.
- Associativity determines the evaluation direction when operators have equal precedence.
- Parentheses can override normal precedence rules.
- Assignment and conditional operators are Right-to-Left associative.
- Most arithmetic and relational operators are Left-to-Right associative.
- Understanding precedence and associativity helps prevent logical errors in programs.
Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.